<?php
	import("view.HTMLTag");
	class TemplateLogic {
		function render($tag,$data,$content,$attributes){
			switch($tag){
				case "flash":
				case "link":
				case "button":
				case "date":
					$result = TemplateLogic::$tag($content,$data,$attributes);
					break;
				case "field":	
				case "file":
				case "pass":
				case "check":
				case "radio":
				case "text":
				case "hidden":
				case "option":
					if(array_key_exists("value",$attributes)===FALSE) $attributes["value"] = "";
					$result = TemplateLogic::input($tag,$content,$data,$attributes);
					break;
				default:
					$result = "error";
					break;
			}
			return $result;
		}
		
		function input($tag,$content,$data,$attributes){
			$name = $attributes["name"];
			$valueKey = $attributes["value"]; //(array_key_exists("value",$attributes)) ? $data->get($attributes["value"]) : "";
			$value = TemplateLogic::content($valueKey,$data);
			$options = Options::parse($attributes["options"]);
			$validate = (array_key_exists("validate",$attributes)) ? $attributes["validate"] : false;
			if($validate){
				if(array_key_exists("class",$options)){
					$options["class"] = "validate(".$validate.") ".$options["class"];
				}else{
					$options["class"] = "validate(".$validate.")";
				}
				
			}
			//$attributes["validate"];
			//var_dump($validate);
			switch($tag){				
				case "check":
					$checked = (array_key_exists("checked",$attributes) && $value==TemplateLogic::content($attributes["checked"],$data));
					$result = check_tag($name,$value,$checked,$options);
					break;
				case "text":
					$result = textarea_tag($name,$value,$options);
					break;
				case "file":
					$result = filefield_tag($name,$options);
					break;
				case "field":
					$result = textfield_tag($name,$value,$options);
					break;
				case "radio":
					$checked = (array_key_exists("checked",$attributes) && $value==TemplateLogic::content($attributes["checked"],$data));
					$result = radio_tag($name,$value,$checked);
					break;
				case "pass":
					$result = password_tag($name,$value,$options);
					break;
				case "option":
					$selected = ($data->get("selected")==$value);
					$result = option_tag($name,$value,$selected);
					break;
				case "hidden":
					$result = hidden_tag($name,$value,$options);
					break;
			}
			return $result;
		}
		
		function content($content,$data){
			$pattern = '/\{(.*?)\}/';
			preg_match_all($pattern, $content, $matches);
			$campos = array();
			$values = array();
			for($m=0;$m<count($matches[0]);$m++){
				$match = $matches[0][$m];
				$object_path = $matches[1][$m];
				if(array_search($match,$campos)===false){
					$campos[] = $match;
					$temp_value = $data->get($object_path);
					/*
					if(array_key_exists("action",$attrs)){
						$functions = explode(",",$attrs["action"]);
						foreach($functions as $function){
							$temp_value = $function($temp_value);
						}
					}
					*/
					$values[] = $temp_value;
				}
			}
			return str_replace($campos,$values,$content);
		}
		function date($content,$data,$attributes){
			$name = TemplateLogic::content($attributes["name"],$data);
			$day = "<select name=\"".$name.":date[day]\"><option value=\"12\">12</option></select>";
			$month = "<select name=\"".$name.":date[month]\"><option value=\"06\">Junio</option></select>";
			$year = "<select name=\"".$name.":date[year]\"><option value=\"2007\">2007</option></select>";
			return $month.$day.$year;
		}
		function link($content, $data, $attributes){
			$to = $attributes["to"];
			$options = Options::parse($attributes["options"]);
			return linkTo($content,TemplateLogic::content($to,$data),$options);
		}
		function flash($content, $data, $attributes){
			$options = Options::parse($attributes["options"]);
			//TemplateLogic::getOptions($attributes["options"]);
			$options["class"] = (array_key_exists("type",$attributes)) ? $attributes["type"] : "error";
			$options["id"] = (array_key_exists("name",$attributes)) ? $attributes["name"] : "error";
			//$options["style"] = "margin: -15px 0px 0px;";
			return div_tag($content,$options)."<script type=\"text/javascript\">var mySlide = new Fx.Slide('".$options["id"]."');mySlide.hide();</script>";
		}

		function button($content,$data,$attributes){
			if(array_key_exists("type",$attributes)){
				$type = $attributes["type"];
			}else if(array_key_exists("to",$attributes)){
				$type = "to";
				$to = TemplateLogic::content($attributes["to"],$data);
			}else{
				$type = "submit";
			}
			
			$result = "";
			$name = TemplateLogic::content($attributes["name"],$data);
			$options = $attributes["options"];
			switch(strToLower($type)){
				case "back":
					$result = buttonBack($name,$options);
					break;
				case "to":
					$result = buttonTo( $name, $to, $options);
					break;
				case "reset":
					$result = buttonReset($name);
					break;
				case "submit":
				default:
					$result = submit_tag($name,$options);
					break;
			}
			return $result;
		}
				
		/*function getOptions($optionString){
			$optionsArray = array();
			if(strLen($optionString)>0){
				$options = explode(";",$optionString);
				for($o=0;$o<count($options);$o++){
					$option = explode(":",$options[$o]);
					$optionsArray[$option[0]] = $option[1];//$this->processContent($option[1]);
				}
			}
			return $optionsArray;
		}*/

		function getAttributes($attributesString){
			$pattern = '/([^\=\s]*?)\=([\'\"])(.*?)\2/s';
			$attributes = array();
			preg_match_all($pattern,trim($attributesString),$attributesMatch);
			for($a=0;$a<count($attributesMatch[0]);$a++){
				$attributes[$attributesMatch[1][$a]] = $attributesMatch[3][$a];
			}
			return $attributes;
		}
	}	
?>